package com.linked.basemanage.permissioncontrol.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.permissioncontrol.bean.po.MenuInfoPO;
import com.linked.basemanage.permissioncontrol.service.IMeunInfoService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/2/22 14:03
 * @description : desc
 */
@Slf4j
@RestController
@RequestMapping("menuinfo")
public class MenuInfoController {

    private final ObjectMapper mapper;
    private final IMeunInfoService meunInfoService;

    @Autowired
    public MenuInfoController(ObjectMapper mapper, IMeunInfoService meunInfoService) {
        this.mapper = mapper;
        this.meunInfoService = meunInfoService;
    }

    @PostMapping("addMenu")
    Result addMenu(@RequestBody MenuInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("新增菜单接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = meunInfoService.addMenu(param);
        } catch (Exception e) {
            log.error("新增菜单接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

    @PostMapping("queryMenuInfoList")
    Result queryMenuInfoList(@RequestBody MenuInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("查询菜单信息接口 入参：{}", mapper.writeValueAsString(param));
        }
        List<MenuInfoPO> menuList = null;
        try {
            menuList = meunInfoService.queryMenuInfoList(param);
        } catch (Exception e) {
            log.error("查询菜单信息接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return !CollectionUtils.isEmpty(menuList) ? Result.success(menuList) : Result.ok(false, "未查询到数据！");
    }
}
